﻿using DevExpress.XtraGrid.Views.Grid;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Resources;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraRichEdit;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace NAGFwk.UI
{
    public partial class RibbonBaseCustomEditForm : EditFormUserControl
    {
        public bool IsNew { get; set; }
        public bool IsEdit { get; set; }        

        public RibbonBaseCustomEditForm()
        {
            InitializeComponent();
        }

        public void SetReadOnly(bool IsReadOnly)
        {
            TraverseControls(this, IsReadOnly);
        }

        private void TraverseControls(Control c, bool IsReadOnly)
        {
            CheckEditors(c, IsReadOnly);
            CheckRichEditControl(c, IsReadOnly);
            CheckGridControl(c, IsReadOnly);
            CheckOther(c, IsReadOnly);
        }
        private void CheckOther(Control c, bool IsReadOnly)
        {
            foreach (Control control in c.Controls)
                TraverseControls(control, IsReadOnly);
        }
        private void CheckEditors(Control c, bool IsReadOnly)
        {
            if (c is BaseEdit)
                ((BaseEdit)c).Properties.ReadOnly = IsReadOnly;
        }
        private void CheckRichEditControl(Control c, bool IsReadOnly)
        {
            if (c is RichEditControl)
                ((RichEditControl)c).ReadOnly = IsReadOnly;
        }
        private void CheckGridControl(Control c, bool IsReadOnly)
        {
            if (c is GridControl)
            {
                GridControl gridControl = (GridControl)c;

                for (int j = 0; j < gridControl.ViewCollection.Count; j++)
                {
                    if (gridControl.ViewCollection[j] is GridView)
                    {
                        (gridControl.ViewCollection[j] as GridView).OptionsBehavior.Editable = !IsReadOnly;
                        continue;
                    }

                    if (gridControl.ViewCollection[j] is LayoutView)
                    {
                        (gridControl.ViewCollection[j] as LayoutView).OptionsBehavior.Editable = !IsReadOnly;
                        continue;
                    }

                    if (gridControl.ViewCollection[j] is CardView)
                    {
                        (gridControl.ViewCollection[j] as CardView).OptionsBehavior.Editable = !IsReadOnly;
                        continue;
                    }

                    if (gridControl.ViewCollection[j] is BandedGridView)
                    {
                        (gridControl.ViewCollection[j] as BandedGridView).OptionsBehavior.Editable = !IsReadOnly;
                        continue;
                    }
                }
            }
        }
    }
}
